// @flow
import React, { Component } from 'react'
import { Form, Input, Select, InputNumber } from 'antd'
import universalFetch, { handleFetchError } from 'store/modules/fetch'

const FormItem = Form.Item
const Option = Select.Option

const formItemLayout = {
  labelCol: {
    xs: { span: 24 },
    sm: { span: 6 }
  },
  wrapperCol: {
    xs: { span: 24 },
    sm: { span: 14 }
  }
}
type Props = {
  form: Object
}
type State = {
  sensorType: Array<Object>
}
class PointModal extends Component {
  props: Props
  state: State
  getSensorType: Function
  constructor (props: Props) {
    super(props)
    this.state = {
      sensorType: []
    }
    this.getSensorType = this.getSensorType.bind(this)
  }
  componentWillMount () {
    this.getSensorType()
  }
  getSensorType () {
    universalFetch(`${__SERVICE_API__}sys/dicts?pageNum=1&type=sensorType`)
    .then(res => res.json())
    .then((json) => {
      if (json.code !== 200000) {
        throw new Error(JSON.stringify({ code: json.code, message: json.message }))
      }
      console.log(json)
      this.setState({
        sensorType: json.result
      })
    })
    .catch((error) => {
      handleFetchError(error)
      console.error(error)
    })
  }
  handleSubmitComponent () {}
  checkNum (rule, value, callback) {
    const checkNum = /^[0-9]+$/
    const checkReg = /^[a-zA-Z0-9_]*$/
    if (value && !checkReg.test(value)) {
      callback('只能输入英文、数字和下划线')
    } else {
      if (checkNum.test(value)) {
        callback('不能是纯数字')
      } else {
        callback()
      }
    }
  }
  render () {
    const { getFieldDecorator } = this.props.form
    const { sensorType } = this.state
    return (
      <Form onSubmit={this.handleSubmitComponent}>
        <FormItem {...formItemLayout} label='传感器类型' hasFeedback>
          {getFieldDecorator('sensorType', {
            rules: [
              { required: true, message: '请填写传感器类型' }
            ]
          })(
            <Select showSearch
              filterOption={(input, option) => {
                return option.props.children.indexOf(input) >= 0
              }} placeholder='请填写传感器类型'>
              {
                sensorType.map((d, i) => {
                  return <Option key={i} value={d.value}>
                    {d.label}
                  </Option>
                })
              }
            </Select>
          )}
        </FormItem>
        <FormItem {...formItemLayout} label='传感器名字' hasFeedback>
          {getFieldDecorator('sensorName', {
            rules: [
              { required: true, message: '请填写传感器名字', whitespace: true }
            ]
          })(
            <Input placeholder='请填写传感器名字' />
          )}
        </FormItem>
        <FormItem {...formItemLayout} label='表名' hasFeedback>
          {getFieldDecorator('nameEn', {
            rules: [
              { required: true, message: '请填写表名', whitespace: true },
              { validator: this.checkNum }
            ]
          })(
            <Input placeholder='请填写表名' />
          )}
        </FormItem>
        <FormItem {...formItemLayout} label='通道ID' hasFeedback>
          {getFieldDecorator('channelId', {
            rules: [
              { required: true, message: '请填写通道ID', whitespace: true }
            ]
          })(
            <InputNumber style={{ width:284 }} />
          )}
        </FormItem>
      </Form>
    )
  }
}

export default Form.create()(PointModal)
